package com.kuang.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.entity.Achievement;
import com.kuang.entity.Params;
import com.kuang.entity.SysUser;
import com.kuang.mapper.AchievementMapper;
import com.kuang.mapper.SysUserMapper;
import com.kuang.service.AchievementService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AchievementServiceImpl implements AchievementService {

    @Autowired
    private AchievementMapper achievementMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 根据学生 学号 类别 查询学生个人转换信息
     */
    @Override
    public List<Achievement> getAchievementId(String s_number, int status) {
        List<Achievement> data = achievementMapper.getAchievementId(s_number, status);
        return data;
    }

    @Override
    public void addAchievement(Achievement id) {
        achievementMapper.addAchievement(id);

    }


    /**
     * 认定成果查询（根据类别，学号查询）
     */
    @Override
    public List<Achievement> findAchievementByType(String s_number, int status) {
        List<Achievement> data = achievementMapper.findAchievementByType(s_number, status);

        return data;
    }

    /**
     * 修改成果表信息
     * */
    @Override
    public int modifyAchievementById(Achievement achievement) {
        int row = achievementMapper.modifyAchievementById(achievement);
        return row;
    }


    /**
     * 教师系统：学习成果认定分页查询
     **/
    @Override
    public PageInfo TeFindBySearchAffirm(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Achievement> list = achievementMapper.TeFindBySearchAffirm(params);
        return PageInfo.of(list);
    }
    /**
     * 教师系统：学习成果转换分页查询
     **/
    @Override
    public PageInfo TeFindBySearchTransition(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Achievement> list = achievementMapper.TeFindBySearchTransition(params);
        if(CollUtil.isEmpty(list)){
            return PageInfo.of(new ArrayList<>());
        }
        for(Achievement achievement : list){
            if (ObjectUtil.isNotEmpty(achievement.getUsername())){
                SysUser sysUser = sysUserMapper.selectByUsername(achievement.getUsername());
                if (ObjectUtil.isNotEmpty(sysUser)){
                    achievement.setScoresS(sysUser.getScores());
                }
            }
        }
        return PageInfo.of(list);
    }
    /**
     * 管理员系统：学习成果认定分页查询
     **/
    @Override
    public PageInfo findBySearchAffirm(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Achievement> list = achievementMapper.findBySearchAffirm(params);
        return PageInfo.of(list);
    }
    /**
     * 管理员系统：学习成果转换分页查询
     **/
    @Override
    public PageInfo findBySearchTransition(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Achievement> list = achievementMapper.findBySearchTransition(params);
        if(CollUtil.isEmpty(list)){
            return PageInfo.of(new ArrayList<>());
        }
        for(Achievement achievement : list){
            if (ObjectUtil.isNotEmpty(achievement.getUsername())){
                SysUser sysUser = sysUserMapper.selectByUsername(achievement.getUsername());
                if (ObjectUtil.isNotEmpty(sysUser)){
                    achievement.setScoresS(sysUser.getScores());
                }
            }
        }
        return PageInfo.of(list);
    }
    /**
     * 认定审核通过
     * **/
    @Override
    public void SuccessfullyApprovedAchievement(Achievement achievement) {
        achievementMapper.SuccessfullyApprovedAchievement(achievement);
        System.out.println(achievement);

    }
    /**
     * 认定审核不通过
     * **/
    @Override
    public void failApprovedAchievement(Achievement achievement) {
        achievementMapper.failApprovedAchievement(achievement);
    }
     /**
     * 认定审核通过
     * **/
    @Override
    public void PassTransformationOfLearningOutcomes(Achievement achievement) {
        achievementMapper.PassTransformationOfLearningOutcomes(achievement);;
        System.out.println(achievement);

    }
    /**
     * 认定审核不通过
     * **/
    @Override
    public void FailTransformationOfLearningOutcomes(Achievement achievement) {
        achievementMapper.FailTransformationOfLearningOutcomes(achievement);
    }

}
